package controller;

import model.Blog;
import model.User;
import model.blogMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/delete")
public class DeleteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        HttpSession session=req.getSession(false);
        if(session==null){
            resp.setStatus(403);
            return;
        }
        User user=(User)session.getAttribute("user");
        if(user==null){
            resp.setStatus(403);
        }
        String blogId=req.getParameter("blogId");
        if(blogId==null||blogId.equals("")){
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("blogId未正常获取(前端)");
            return;
        }
        blogMapper blogMapper=new blogMapper();
        try {
            blogMapper.delete(Integer.parseInt(blogId));
            resp.sendRedirect("blog_List.html");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
